package com.ten.mall.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ten.mall.service.PrivilegeService;
import com.ten.mall.service.RoleService;
import com.ten.mall.vo.Privilege;
import com.ten.mall.vo.PrivilegeVo;
import com.ten.mall.vo.Role;
@Controller
public class RoleController {
	@Autowired
	private RoleService pService;
	@Autowired
	private PrivilegeService privilegeService;
	@RequestMapping("/admin/roleList")
	public String userList(@RequestParam(name="start",defaultValue="1") Integer start,
			   				@RequestParam(name="size",defaultValue="10") Integer size,
			   				HttpServletRequest res,String name){
		Map<String,Object> map =pService.selectByPages(start, size, name);
		List<Role> list =(List<Role>) map.get("list");
		Long mySize=(Long) map.get("total");
		int total = mySize.intValue()/10+1;
		res.setAttribute("list", list);
		res.setAttribute("total", total);
		res.setAttribute("page", start);
		return "jsp/roleList";
	}
	
	@RequestMapping("/admin/toRole")
	public String toAdd(Integer id,Model model){
		if(id!=null){
			Role p =pService.selectById(id);
			model.addAttribute("p", p);
		}
		return "jsp/roleAdd";
	}
	
	@RequestMapping("/admin/saveRole")
	@ResponseBody
	public String addUser(Role p){
		String rs ="error";
		try {
			if(p.getId()!=null){
				pService.updateRole(p);
			}else{
				pService.insertRole(p);
			}
			rs="ok";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;
	}
	@RequestMapping("/admin/delRole")
	@ResponseBody
	public String delRole(Integer id){
		String rs ="ok";
		try {
			pService.deleteRole(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			rs="error";
		}
		return rs;
	}
	
	@RequestMapping("/admin/addRolePrilige")
	public String addPrilige(Integer id,HttpServletRequest res){
		Role role =pService.selectById(id);
		List<Privilege> list =privilegeService.selectAll();
		res.setAttribute("role", role);
		res.setAttribute("list", list);
		return "jsp/addPrilige";
	}
	/**
	 * 角色添加权限
	 * @param r
	 * @param privilege
	 * @return
	 */
	@RequestMapping("/admin/addPrivilege")
	@ResponseBody
	public String addPrivilege(Role r,String privilege){
		String rs ="ok";
		try {
			String str[] =privilege.split(",");
			privilegeService.deletMyRole(r.getId());
			List<PrivilegeVo> list =new  ArrayList<>();
				for(String s:str){
					PrivilegeVo vo =new PrivilegeVo();
					vo.setPrivilegeId(Integer.parseInt(s));
					vo.setRoleId(r.getId());
					vo.setRoleName(r.getRoleName());
					list.add(vo);
				}	
			pService.insertList(list);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			rs="error";
		}
		return rs;
	}
}